package com.edgar.animation;

import android.app.Activity;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.edgar.main.R;
import com.edgar.service.PushService;
import com.edgar.utils.DatabaseUtils;

import com.edgar.animation.StretchAnim.AnimationListener;

public class AnimationMain extends  Activity {


	Button alphaBtn ;
	Button scaleBtn	;
	Button rotateBtn ;
	Button translateBtn ;
	Button setBtn;
	
	TextView tv;
	
	Animation alpaAnimation;
	Animation scaleAnimation;
	Animation rotateAnimation;
	Animation translateAnimation;
	Animation setAnimation;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_main);
		
//		initCtrl();
		initView();
		initAnimation();
		initBtnListener();
	}
	
	private void initStretchAnimation(View v,int start,int end){
		
//		StretchAnimation  stretchanimation = new StretchAnimation(start, end, StretchAnimation.TYPE.vertical, 500); 
		StretchAnim stretchanimation = new StretchAnim(start, end, StretchAnim.TYPE.vertical, 500); 
	    stretchanimation.setInterpolator(null);  
	    stretchanimation.setDuration(200);  
	    stretchanimation.setOnAnimationListener(l);  
	    stretchanimation.startAnimation(v);
	}
	private AnimationListener l = new AnimationListener(){
		 @Override  
		    public void animationEnd(View v) {  
		        Log.i(getClass().getName(), ("-----"+((Button)v).getText().toString())+" annation end");  
		    }  
	};
 
	private void initAnimation()
	{
		alpaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
		scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
		rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
		translateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
		setAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_set);
	}
	
	private void initBtnListener()
	{
		alphaBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				tv.startAnimation(alpaAnimation);
				initStretchAnimation(scaleBtn,300,1);
			}
		});
		
		scaleBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tv.startAnimation(scaleAnimation);
			}
		});
		
		rotateBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tv.startAnimation(rotateAnimation);
				
			}
		});
		
		translateBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				tv.startAnimation(translateAnimation);
				initStretchAnimation(scaleBtn,1,300);
			}
		});
		
		setBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tv.startAnimation(setAnimation);
			}
		});
	}
	
	private void initView(){
		
//		alphaBtn = (Button)findViewById(R.id.btn_alpha_anim);
//		scaleBtn = (Button)findViewById(R.id.btn_scale_anim);
//		rotateBtn= (Button)findViewById(R.id.btn_rotate_anim);
//		translateBtn= (Button)findViewById(R.id.btn_translate_anim);
//		setBtn = (Button)findViewById(R.id.btn_set_anim);
//		tv  = (TextView)findViewById(R.id.tv);
		
		
		RelativeLayout maiview = new RelativeLayout(this);
		RelativeLayout.LayoutParams lp_main = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
		maiview.setLayoutParams(lp_main);
			
		alphaBtn= new Button(this); 
		alphaBtn.setText("alpha");
		alphaBtn.setId(1); 
		
		RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,200); 
		lp1.setMargins(10, 10, 10, 10);
		maiview.addView(alphaBtn, lp1 );   
			
		scaleBtn = new Button(this); 
		scaleBtn.setText("scale");
		scaleBtn.setId(2); 
		 
		RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,300); 
		lp2.setMargins(10, 10, 10, 10);
		lp2.addRule(RelativeLayout.RIGHT_OF, 1);
		maiview.addView(scaleBtn, lp2 );
		
		rotateBtn = new Button(this); 
		rotateBtn.setText("rotate");
		rotateBtn.setId(3); 
		RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
		lp3.setMargins(10, 10, 10, 10);
		lp3.addRule(RelativeLayout.RIGHT_OF,2);
		maiview.addView(rotateBtn, lp3 );
		
		translateBtn = new Button(this); 
		translateBtn.setText("translate");
		translateBtn.setId(4); 
		RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
		lp4.setMargins(10, 10, 10, 10);
		lp4.addRule(RelativeLayout.RIGHT_OF, 3);
		maiview.addView(translateBtn, lp4 );
		
		setBtn = new Button(this); 
		setBtn.setText("set");
		setBtn.setId(5); 
		RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
		lp5.setMargins(10, 10, 10, 10);
		lp5.addRule(RelativeLayout.BELOW, 1);
		maiview.addView(setBtn, lp5 );

		tv = new TextView(this); 
		tv.setText("Animation Show");
		tv.setId(6); 
		RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(300,600); 
//		lp6.addRule(RelativeLayout.BELOW, 1);
		lp6.addRule(RelativeLayout.BELOW, 2);
//		lp6.addRule(RelativeLayout.RIGHT_OF, 5);
		tv.setBackgroundColor(Color.BLUE);
		maiview.addView(tv, lp6 ); 

	    setContentView(maiview);
	    

	}

}
